package cn.wx.scholar;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import org.apache.commons.lang.StringUtils;

import com.csvreader.CsvReader;

import cn.wx.common.utils.JSONUtils;

public class FosExpertC {

	public static void main(String[] args) {
		
		final String filePath = "E:\\my_read_csv\\org.csv";
		try {
			StringBuffer sb =null;
			BufferedWriter output = null;
			File file = new File(filePath);
	        FileInputStream fileInputStream = new FileInputStream(file);  
	        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			

    		File of = new File("E:\\my_read_csv\\org_g.csv");
			FileOutputStream ofs = new FileOutputStream(of,false);
			OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
			output = new BufferedWriter(ofw);

			String h = "\"fId\",\"name\",\"latitude\",\"longitude\"";
			
			sb = new StringBuffer();
			sb.append(h);
			
			output.write(sb.toString());
			output.newLine();
	        
			CsvReader csvReader = new CsvReader(bufferedInputStream,Charset.forName("utf-8"));
			
			
			// 读表头
            csvReader.readHeaders();
            int a = 0;
            while (csvReader.readRecord()){
            	
            	System.out.println(a++);
            	
        			
            	
				/**
				 * 解析csv
				 */
				String fId = csvReader.get(0);
				String name = csvReader.get(1);
				String latitude = csvReader.get(2);
				String longitude = csvReader.get(3);

				
				sb = new StringBuffer();
				sb.append(JSONUtils.c(fId)).append(",")
				.append(JSONUtils.c(name)).append(",")
				.append(JSONUtils.c(latitude)).append(",")
				.append(JSONUtils.c(longitude)).append(",");
				
				
				output.write(sb.toString());
				output.newLine();
				
				output.flush();

            }
			
            output.flush();
            output.close();
            output = null;
            
            /**
			 * 关闭读入流
			 */
			csvReader.close();
			csvReader = null;
            
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
